import 'package:flutter/material.dart';
class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("列表"), // 标题
          leading: Icon(Icons.menu), // 左侧图标
          actions: [Icon(Icons.settings)],  // 右侧图标数组
          elevation: 0.0,// 去掉阴影
          centerTitle: true, // 标题居中

        ),
        body:ListViewDemo()
    );
  }
}

class ListViewDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: [
          ListViewBasic(),
          ListViewHorizontal(),
          ListViewBuilder(),
          ListViewSeperatedDemo(),
        ],
      ),
    );
  }
}

class ListViewBasic extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200,
      child:ListView(
        // 垂直滚动
        children: [
          ListTile(
            leading: Icon(Icons.access_alarm,size: 50),
            title:Text("access_alarm"),
            subtitle: Text('子标题'),
            trailing: Icon(Icons.keyboard_arrow_right),
          ),
          ListTile(
            leading: Icon(Icons.supervised_user_circle,size: 50),
            title:Text("access_alarm"),
            subtitle: Text('子标题'),
            trailing: Icon(Icons.keyboard_arrow_right),
            selected: true, // 被选中
            selectedTileColor: Colors.red[100],
          ),
          ListTile(
            leading: Icon(Icons.ac_unit,size: 50),
            title:Text("access_alarm"),
            subtitle: Text('子标题'),
            trailing: Icon(Icons.keyboard_arrow_right),
          ),
          ListTile(
            leading: Icon(Icons.add_photo_alternate,size: 50),
            title:Text("access_alarm"),
            subtitle: Text('子标题'),
            trailing: Icon(Icons.keyboard_arrow_right),
          )
        ],

      ),
    );
  }
}

class ListViewHorizontal extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 100,
      child: ListView(
        scrollDirection: Axis.horizontal,
        children: [
          Container(
            width: 160,
            color: Colors.amber,
          ),
          Container(
            width: 160,
            color: Colors.blue,
          ),
          Container(
            width: 160,
            color: Colors.purple,
          ),
          Container(
            width: 160,
            color: Colors.grey,
          )
        ],
      ),
    );
  }
}

class ListViewBuilder extends StatelessWidget {
  final List<Widget> users = new List<Widget>.generate(20, (index) => OutlinedButton(onPressed: (){}, child: Text('姓名$index')));
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 150,
      child: ListView.builder(
        itemCount: users.length,
        itemExtent: 30,
        itemBuilder: (context,index){
          return users[index];

        },
      ),
    );
  }
}

class ListViewSeperatedDemo extends StatelessWidget {
  final List<Widget> products = List.generate(20, (index) => ListTile(
    leading: Image.asset('images/men.png'),
    title: Text('商品标题$index'),
    subtitle: Text("子标题"),
    trailing: Icon(Icons.keyboard_arrow_right),

  ));
  @override
  Widget build(BuildContext context) {
    // 偶数分割线
    Widget dividerOdd = Divider(
      color: Colors.blue,
      thickness: 2,
    );
    // 单数分割线
    Widget dividerEven = Divider(
      color: Colors.red,
      thickness: 2,
    );
    return Column(
      children: [
        ListTile(
          title:Text('商品列表')
        ),
        Container(
          height: 200,
          child: ListView.separated(
            itemCount: products.length,
            itemBuilder: (context,index){
              return products[index];
            },
            // 分割器的构造器
            separatorBuilder:(context,index){
              return index%2 == 0?dividerEven:dividerOdd;
            } ,
          ),
        )
      ],
    );
  }
}




